%m1 = [2.79 2.44 1 1 1 1 1 1 1 1 1 1];
%m2 = [-0.28 4.54 1 1 1 1 1 1 1 1 1 1];
%m3 = [m1 ; m2];
clear;
mix = [8 16];
iter = [2 4 6 8 10];
combo = 0;
file_1 = fopen('results_overall.txt','w')
for m = 1:size(mix,2)
    for it = 1:size(iter,2) 
        gmm_mixtures =mix(m);
        iterations = iter(it);

        initial_means = ones(gmm_mixtures,12);
        %initial_means = (rand(4,12)  - 0.5) .* 4;
        initial_variance = ones(gmm_mixtures,12);
        initial_weights = ones(gmm_mixtures,1) * (1/gmm_mixtures);

        user(1000).name = '';
        user(1000).mean = [];
        user(1000).variance = [];
        user(1000).weights = [];

        test_user(1000).name = '';
        test_user(1000).test_vectors = cell(1,100);

        folders = pathlist(genpath('TIMIT/TRAIN/'));
        count_users = 0;
        for f = 1:size(folders,1)
           folder = folders(f);

           wavs = dir(strcat(char(folder),'/*.WAV'));

           if (size(wavs,1) > 0)
            folder = folder
            count_users = count_users + 1;
            user(count_users).name = char(folder);
            test_user(count_users).name = char(folder);

            audio_data = [];
            Fs = [];

            %first 20 percent of wav files set aside for testing

            set_aside_for_test = size(wavs,1) * 0.2;

            for t_w = 1:uint8(set_aside_for_test)
               t_w = t_w;
               [raw_data_test Fs] =  readsph(strcat(char(folder),'/',wavs(t_w).name));
               mfcc_of_test_file = melcepst(raw_data_test,8000);
               test_user(count_users).test_vectors{t_w} = mfcc_of_test_file;
            end


            %remaining wav files used for training

            for w = uint8(set_aside_for_test)+1:size(wavs,1)
                w =w;
                [raw_data Fs] =  readsph(strcat(char(folder),'/',wavs(w).name));
                audio_data = [audio_data;  raw_data];
            end

               mfcc_audio_data = melcepst(audio_data,8000);
               rand_index = uint32(rand(gmm_mixtures,1) * size(mfcc_audio_data,1));
               rand_index(rand_index == 0) = [1];
               initial_means = mfcc_audio_data(rand_index',:);
               [gmm_mu,gmm_sigma,gmm_weights] = gaussmix(mfcc_audio_data,[], iterations,initial_means, initial_variance, initial_weights);
               user(count_users).mean = gmm_mu;
               user(count_users).variance = gmm_sigma;
               user(count_users).weights = gmm_weights;
           end

        end

        user(count_users+1:1000) = [];
        user = user

        test_user(count_users+1:1000) = [];
        test_user = test_user


        count_of_total_test_vectors = 0;
        count_of_correct_test_vectors = 0;
        for t = 1:size(test_user,2)

            disp(sprintf(' testing %d of %d ', t, size(test_user,1)));
            test_vectors = test_user(t).test_vectors;
            for t_v = 1:size(test_user(t).test_vectors,2)
                test_mfcc = test_user(t).test_vectors{t_v};
                max_mean_lp_user = [];
                max_mean_lp = -Inf;
                for u = 1:size(user,2)
                    [lp rp kh kp] = gaussmixp(test_mfcc,user(u).mean, user(u).variance, user(u).weights);
                    mean_lp = mean (lp);
                    if(mean_lp > max_mean_lp)
                        max_mean_lp = mean_lp;
                        max_mean_lp_user = user(u);
                    end
                end
                count_of_total_test_vectors = count_of_total_test_vectors + 1;
                if( strcmp(test_user(t).name,max_mean_lp_user.name) == 1)
                    count_of_correct_test_vectors = count_of_correct_test_vectors +1;
                end
            end
        end
        count_of_correct_test_vectors = count_of_correct_test_vectors
        count_of_total_test_vectors = count_of_total_test_vectors
        combo  = combo + 1;
        %accuracy_report{combo} = sprintf(' mixtures = %d iterations = %d accuracy = %f ', gmm_mixtures, iterations,count_of_correct_test_vectors/ count_of_total_test_vectors)
        fprintf(file_1,'%d\t%d\t%f\n',gmm_mixtures,iterations,count_of_correct_test_vectors/count_of_total_test_vectors);
    end
end
fclose(file_1)


  
    
  




